package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.OrderDiscuss;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.OrderDiscussService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/orderDiscuss")
public class OrderDiscussController {
    @Autowired
    private OrderDiscussService orderDiscussService;

    // 添加新的评论
    @PostMapping("/")
    public Result addOrderDiscuss(@RequestBody OrderDiscuss orderDiscuss) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        orderDiscuss.setUserName(username);
        orderDiscuss.setUpdateTime(LocalDateTime.now());
        orderDiscussService.addOrderDiscuss(orderDiscuss);

        if(orderDiscuss.getDiscussType() == 1){
            // 回复数 + 1
            orderDiscussService.addRepluNum(orderDiscuss.getReplyId());
        }
        return new Result(true, StatusCode.OK,"添加成功");
    }

    // 获取所有评论列表
    @GetMapping("/{id}")
    public Result<List<OrderDiscuss>> getOrderDiscussList(@PathVariable Integer id) {
        List<OrderDiscuss> discussList = orderDiscussService.getOrderDiscussList(id);
        for (OrderDiscuss discuss : discussList){
            Integer tmp = discuss.getOrderDiscussId();
            List<OrderDiscuss> temp = orderDiscussService.getOrderDiscussWithRepliesById(tmp);

            for(int i = 0; i < temp.size(); i++){
                OrderDiscuss replyDiscuss = temp.get(i);
                List<OrderDiscuss> replyDiscusses = orderDiscussService.selectAllReplyDiscuss(replyDiscuss.getOrderDiscussId());

                // 下面把replyDiscusses插入到temp中，不要插在最后，插在replyDiscuss后面
                int index = temp.indexOf(replyDiscuss) + 1; // 获取replyDiscuss在temp中的索引并加1
                temp.addAll(index, replyDiscusses); // 插入replyDiscusses列表到temp中
            }

            discuss.setReplyComments(temp);

        }
        return new Result(true, StatusCode.OK,"获取所有评论列表成功",discussList);

    }

    // 获取用户点赞列表
    @GetMapping("/userLike")
    public Result<List<Integer>> userLikeList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        return new Result<>(true,StatusCode.OK,"查询成功",orderDiscussService.userLikeList(username));
    }

    // 更新评论信息
    @PutMapping("/")
    public Result updateOrderDiscuss(@RequestBody OrderDiscuss orderDiscuss) {
        orderDiscuss.setUpdateTime(LocalDateTime.now());
        orderDiscussService.updateOrderDiscuss(orderDiscuss);
        return new Result(true, StatusCode.OK,"更新评论信息成功");
    }

    // 删除评论
    @DeleteMapping("/{id}")
    public Result deleteOrderDiscuss(@PathVariable Integer id) {
        orderDiscussService.deleteOrderDiscuss(id);
        return new Result(true, StatusCode.OK,"删除评论成功");
    }

    // 根据用户名获取评论回复
    @GetMapping("/user/{userName}")
    public Result<List<OrderDiscuss>> getDiscussRepliesByUsername(@PathVariable String userName) {
        return new Result(true, StatusCode.OK,"根据用户名获取评论回复成功",orderDiscussService.getDiscussRepliesByUsername(userName));
    }

    // 根据回复ID获取评论回复
    @GetMapping("/reply/{replyId}")
    public Result<List<OrderDiscuss>> getDiscussRepliesByReplyId(@PathVariable String replyId) {
        return new Result(true, StatusCode.OK,"根据用户名获取评论回复成功",orderDiscussService.getDiscussRepliesByReplyId(replyId));
    }

    @GetMapping("/aaa/{id}")
    public List<OrderDiscuss> aaa(@PathVariable Integer id){
        return orderDiscussService.getOrderDiscussWithRepliesById(id);
    }

    @PostMapping("/addLike/{orderDiscussId}")
    public Result<Integer> addLike(@PathVariable Integer orderDiscussId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        return new Result<>(true,StatusCode.OK,"点赞成功",orderDiscussService.addLike(username,orderDiscussId));
    }

    @DeleteMapping("/deleteLike/{orderDiscussId}")
    public Result<Integer> deleteLike(@PathVariable Integer orderDiscussId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        return new Result<>(true,StatusCode.OK,"点赞成功",orderDiscussService.deleteLike(username,orderDiscussId));
    }
}
